using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CustomLitJson;
using System;


public static class BuildManifestManager 
{
    static Dictionary<string, BuildEnvInfo> nameToEnvInfoDic;

    public static void Load()
    {
        var asset = Resources.Load<TextAsset>("build-manifest");
        var json = asset.text;
        Debug.Log(json);
        nameToEnvInfoDic = JsonMapper.Instance.ToObject<Dictionary<string, BuildEnvInfo>>(json);
    }

    public static void TryLoad()
    {
        Debug.Log("TryLoad");
        if (nameToEnvInfoDic == null)
        {
            Load();
        }
    }

    public static BuildEnvInfo GetEnvInfo(string name)
    {
        TryLoad();
        var info = nameToEnvInfoDic.TryGet(name);
        if(info == null)
        {
            throw new System.Exception("[BuildManifestManager] not found env info for " + name);
        }
        return info;
    }
}

public class BuildEnvInfo
{
    public string type;

    // for sftp
    public string host;
    public string username;
    public string password;
    public string remoteDefaultPath;

    // for obs
    public string endpoint;
    public string accessKey;
    public string secretKey;
    public string bucketName;

    public Transporter CreateTransporterWithDefaultRemotePath()
    {
        var transporter = this.CreateTransporter();
        transporter.ChangeDirectory(remoteDefaultPath);
        return transporter;
    }

    Transporter CreateTransporter()
    {
        if (this.type == "sftp")
        {
            var host = this.host;//"47.100.253.122";
            var username = this.username; // "root";
            var password = this.password; // "01313015@com";
            var remoteDefaultPath = this.remoteDefaultPath; // "/var/www/html";
            var client = new SftpTransporter(host, username, password);
            Debug.Log("Use sftp");
            return client;
        }
        else if (this.type == "obs")
        {
            var endpoint = this.endpoint;
            var ak = this.accessKey;
            var sk = this.secretKey;
            var bucketName = this.bucketName;
            var client = new ObsTransporter(endpoint, ak, sk, bucketName);
            Debug.Log("Use obs");
            return client;
        }
        else
        {
            throw new Exception("not implement for build env type: " + this.type);
        }
    }
}
